home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / xshp15.zip / DRAWPOBJ.C < prev    next >
Text File  |  1991-12-01  |  3KB  |  66 lines

  1. /* Draws all visible faces in the specified polygon-based object.  The
  2.    object must have previously been transformed and projected, so that
  3.    the ScreenVertexList array is filled in. */
  4. #include "polygon.h"
  5.  
  6. void DrawPObject(PObject * ObjectToXform)
  7. {
  8.    int i, j, NumFaces = ObjectToXform->NumFaces, NumVertices;
  9.    int * VertNumsPtr;
  10.    Face * FacePtr = ObjectToXform->FaceList;
  11.    Point * ScreenPoints = ObjectToXform->ScreenVertexList;
  12.    long v1, v2, w1, w2;
  13.    Point Vertices[MAX_POLY_LENGTH];
  14.    PointListHeader Polygon;
  15.  
  16.    /* Draw each visible face (polygon) of the object in turn */
  17.    for (i=0; i<NumFaces; i++, FacePtr++) {
  18.       NumVertices = FacePtr->NumVerts;
  19.       /* Copy over the face's vertices from the vertex list */
  20.       for (j=0, VertNumsPtr=FacePtr->VertNums; j<NumVertices; j++)
  21.          Vertices[j] = ScreenPoints[*VertNumsPtr++];
  22.       /* Draw only if outside face showing (if the normal to the
  23.          polygon points toward the viewer; that is, has a positive
  24.          Z component) */
  25.       v1 = Vertices[1].X - Vertices[0].X;
  26.       w1 = Vertices[NumVertices-1].X - Vertices[0].X;
  27.       v2 = Vertices[1].Y - Vertices[0].Y;
  28.       w2 = Vertices[NumVertices-1].Y - Vertices[0].Y;
  29.       if ((v1*w2 - v2*w1) > 0) {
  30.          /* It is facing the screen, so draw */
  31.          /* Appropriately adjust the extent of the rectangle used to
  32.             erase this object later */
  33.          for (j=0; j<NumVertices; j++) {
  34.             if (Vertices[j].X >
  35.                   ObjectToXform->EraseRect[NonDisplayedPage].Right)
  36.                if (Vertices[j].X < SCREEN_WIDTH)
  37.                   ObjectToXform->EraseRect[NonDisplayedPage].Right =
  38.                         Vertices[j].X;
  39.                else ObjectToXform->EraseRect[NonDisplayedPage].Right =
  40.                      SCREEN_WIDTH;
  41.             if (Vertices[j].Y >
  42.                   ObjectToXform->EraseRect[NonDisplayedPage].Bottom)
  43.                if (Vertices[j].Y < SCREEN_HEIGHT)
  44.                   ObjectToXform->EraseRect[NonDisplayedPage].Bottom =
  45.                         Vertices[j].Y;
  46.                else ObjectToXform->EraseRect[NonDisplayedPage].Bottom=
  47.                      SCREEN_HEIGHT;
  48.             if (Vertices[j].X <
  49.                   ObjectToXform->EraseRect[NonDisplayedPage].Left)
  50.                if (Vertices[j].X > 0)
  51.                   ObjectToXform->EraseRect[NonDisplayedPage].Left =
  52.                         Vertices[j].X;
  53.                else ObjectToXform->EraseRect[NonDisplayedPage].Left=0;
  54.             if (Vertices[j].Y <
  55.                   ObjectToXform->EraseRect[NonDisplayedPage].Top)
  56.                if (Vertices[j].Y > 0)
  57.                   ObjectToXform->EraseRect[NonDisplayedPage].Top =
  58.                         Vertices[j].Y;
  59.                else ObjectToXform->EraseRect[NonDisplayedPage].Top=0;
  60.          }
  61.          /* Draw the polygon */
  62.          DRAW_POLYGON(Vertices, NumVertices, FacePtr->Color, 0, 0);
  63.       }
  64.    }
  65. }
  66.